package com.yoofn.kotlin.base

import android.content.Intent
import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.widget.Toast
import com.yoofn.kotlin.R
import com.yoofn.kotlin.av.view.activity.ReplaceActivity
import com.yoofn.kotlin.av.view.activity.WebActivity
import com.yoofn.kotlin.utils.BarUtils
import com.yoofn.nbwk.base.HttpFramework

/**
 *
 * Created by kalshen on 17/6/2.
 * @描述 activity的基类
 */
abstract class BaseActivity<D : ViewDataBinding, FirstRequestType : Any> : AppCompatActivity(), HttpFramework {
    /**
     * 具有dataBinding 数据绑定特性的 contentView
     * 可以直接调用被layout 包裹的 控件资源id
     * 例:content.textView.text="hello"
     * 不懂的 自己去dataBinding
     */
    lateinit var contentView: D


    /**
     * activity的配置对象
     */
    val viewConfig by lazy {
        setupConfig()
    }

    val swipeRefresh by lazy {
        SwipeRefreshLayout(this).takeIf { viewConfig.shouldSwipeRefresh }
    }


    //activity 的初始化
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置状态栏颜色
        BarUtils.setColor(this, resources.getColor(R.color.colorPrimary))

        val view = LayoutInflater.from(this).inflate(viewConfig.layoutId, swipeRefresh)
        //设置下拉刷新的监听
        swipeRefresh?.setOnRefreshListener { addRequest(viewConfig.netData) }

        setContentView(view)
        contentView = DataBindingUtil.bind(view)

        doInteractive()
    }


    /**
     * 设置activity的配置信息
     * @return 返回activity 的配置对象
     */
    abstract fun setupConfig(): ViewConfig<FirstRequestType>

    /**
     * 处理控件的交互
     */
    abstract fun doInteractive()

    override fun onRequestFinished(what: Int) {
        swipeRefresh?.isRefreshing = false
    }

    /**
     * 物理返回键的 校对初始时间
     */
    var time: Long = 0L

    override fun onBackPressed() {
        val currentTimeMillis = System.currentTimeMillis()
        if (supportFragmentManager.backStackEntryCount == 0) {
            //如果当前activity 不是main activity 执行默认返回操作
            if (currentTimeMillis - time < 2000 || "MainActivity" != this::class.simpleName) {
                finish()
                if ("MainActivity" != this::class.simpleName) {
                    overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right)
                } else {
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_main_out)
                }
            } else {
                time = System.currentTimeMillis()
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show()
            }
        } else {
            supportFragmentManager.popBackStack()
        }
    }



    /**
     * 开启一个replaceActivity
     */
    fun startReplaceActivity(bundle: Bundle) {
        val intent = Intent(this, ReplaceActivity::class.java)
        intent.putExtra(Constants.BUNDLE, bundle)
        startActivity(intent)
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left)
    }

    /**
     * 携带数据的页面跳转
     * @param clz
     * @param bundle
     */
    fun startOtherActivity(clz: Class<*>, bundle: Bundle? = null) {
        val intent = Intent()
        intent.setClass(this, clz)
        if (bundle != null) {
            intent.putExtras(bundle)
        }
        startActivity(intent)
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left)
    }

    /**
     * 开启一个webActivity
     * @param url webView的url地址
     */
    fun startWebActivity(url: String) {
        val intent = Intent(this, WebActivity::class.java)
        intent.putExtra(Constants.Web.URL, url)
        startActivity(intent)
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left)
    }

}
